package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.ActorDao;
import cn.edu.sdjzu.xg.bysj.domain.authority.Actor;
import cn.edu.sdjzu.xg.bysj.domain.authority.ActorAssRole;
import cn.edu.sdjzu.xg.bysj.domain.authority.Role;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;

public final class ActorService {
    private static final ActorDao actorDao = ActorDao.getInstance();
    private static final ActorService actorService = new ActorService();

    public static ActorService getInstance() {
        return actorService;
    }

    //  不会查找一个Actor集合
    //	不会增加一个Actor
    //	不会修改一个Actor
    //	不会删除一个Actor

    public Actor find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Actor actor = actorDao.find(connection, id);
        JdbcHelper.close(connection);
        return actor;
    }

    /**
     * 获得参数Actor对象应对的所有Role，后者又关联着Resource
     *
     * @param actor
     * @return
     * @throws SQLException
     */
    public Collection<Role> getRoles(Actor actor) throws SQLException {
        Collection<ActorAssRole> actorAssRoles = ActorAssRoleService.getInstance().findAll(actor);
        System.out.println(actorAssRoles);
        Collection<Role> roles = new TreeSet<Role>();
        for (ActorAssRole actorAssRole : actorAssRoles) {
            roles.add(actorAssRole.getRole());
        }
        return roles;
    }
}
